<template>
  <el-dialog
    title="更新用户信息"
    :visible.sync="visible"
    width="500px"
    custom-class="user-info-dialog"
    :close-on-click-modal="false"
  >
    <el-form
      :model="userForm"
      :rules="rules"
      ref="userForm"
      label-width="100px"
      class="user-form"
    >
      <!-- 从表单中完全移除用户ID字段的显示 -->
      <el-form-item label="用户账号" prop="username">
        <el-input v-model="userForm.username" placeholder="请输入用户账号"></el-input>
      </el-form-item>
      
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="userForm.name" placeholder="请输入用户名称"></el-input>
      </el-form-item>
      
      <el-form-item label="居住地址" prop="address">
        <el-input v-model="userForm.address" placeholder="请输入居住地址"></el-input>
      </el-form-item>
      
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="userForm.phone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      
      <el-form-item label="班级名称" prop="className">
        <el-input v-model="userForm.className" placeholder="请输入班级名称"></el-input>
      </el-form-item>
      
      <el-form-item label="简介" prop="introduction">
        <el-input
          type="textarea"
          v-model="userForm.introduction"
          :rows="3"
          placeholder="请输入简介"
        ></el-input>
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="userForm.password"
          placeholder="如需修改密码请输入新密码"
          show-password
        ></el-input>
      </el-form-item>
      
      <el-form-item label="身份" prop="role">
        <el-input v-model="userForm.role" disabled></el-input>
      </el-form-item>
    </el-form>
    
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserInfoDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    userData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      loading: false,
      userForm: {
        // id 字段仍然保留在数据中，但不在界面显示
        id: '',
        username: '',
        name: '',
        address: '',
        phone: '',
        className: '',
        introduction: '',
        password: '',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        phone: [
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    userData: {
      handler(val) {
        if (val) {
          // 保持ID值在数据中，但不显示在界面上
          this.userForm = { ...val }
          this.userForm.password = '' // 清空密码字段
        }
      },
      immediate: true
    }
  },
  methods: {
    handleCancel() {
      this.$refs.userForm.resetFields()
      this.$emit('update:visible', false)
    },
    handleConfirm() {
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 提交表单时会包含ID，但用户界面上看不到
          this.$emit('confirm', this.userForm)
          this.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.user-info-dialog {
  .el-dialog__body {
    padding: 30px 20px;
  }
  
  .user-form {
    .el-form-item {
      margin-bottom: 20px;
      
      .el-input__inner {
        border-radius: 4px;
        
        &:focus {
          border-color: #409EFF;
          box-shadow: 0 0 0 2px rgba(64,158,255,.2);
        }
      }
      
      .el-textarea__inner {
        border-radius: 4px;
        
        &:focus {
          border-color: #409EFF;
          box-shadow: 0 0 0 2px rgba(64,158,255,.2);
        }
      }
    }
  }
  
  .dialog-footer {
    text-align: right;
    
    .el-button {
      padding: 9px 20px;
      
      &+.el-button {
        margin-left: 12px;
      }
    }
  }
}

// 添加过渡动画
.el-dialog__wrapper {
  .el-dialog {
    transform: translateY(-20px);
    transition: transform .3s ease-in-out;
  }
  
  &.dialog-fade-enter-active {
    .el-dialog {
      transform: translateY(0);
    }
  }
}
</style> 